<template>
  <div>
    <el-dialog
      :title="{ add: '新增权限', edit: '修改权限' }[mode]"
      :visible="isShow"
      @close="closeEvent"
    >
      <el-form ref="form" :model="form" label-width="100px" :rules="rules">
        <el-form-item
          label="权限名称"
          prop="name"
        ><el-input
          v-model="form.name"
        /></el-form-item>
        <el-form-item
          label="权限标识"
          prop="code"
        ><el-input
          v-model="form.code"
        /></el-form-item>
        <el-form-item
          label="权限描述"
          prop="description"
        ><el-input
          v-model="form.description"
        /></el-form-item>
        <el-form-item label="企业可见" prop="enVisible">
          <el-switch
            v-model="form.enVisible"
            active-text="可见"
            inactive-text="不可见"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: center">
          <el-button type="primary" @click="submit">确定</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { addSysPermission, putSysPermission } from '@/api/permission.js'
export default {
  name: 'DialogItem',
  model: {
    prop: 'isShow'
  },
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      mode: 'add',
      form: {
        enVisible: '0', //	string	非必须
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      },
      rules: {
        enVisible: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        name: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        code: [{ required: true, message: '必填', trigger: 'change' }], //	string	非必须
        description: [{ required: true, message: '必填', trigger: 'change' }] //	string	非必须
      }
    }
  },
  methods: {
    closeEvent() {
      this.$emit('input', false)
      this.mode = 'add' // 还原默认值
      this.$refs.form.resetFields()
      this.form = {
        enVisible: '0', //	string	非必须
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      }
    },
    submit() {
      this.$refs.form.validate(async(result) => {
        if (result) {
          if (this.mode === 'add') {
            await addSysPermission(this.form)
          } else {
            await putSysPermission(this.form)
          }
          this.$message.success(this.mode === 'add' ? '新增成功!' : '修改成功!')
          this.closeEvent()
          this.$emit('getData')
          //   console.log(result)
        }
      })
    }
  }
}
</script>

<style></style>
